package com.gingko.duominuo.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.observe
import com.gingko.duominuo.data.Resource
import com.gingko.duominuo.App
import com.gingko.duominuo.R
import com.gingko.duominuo.data.request.RegisterReqVo
import com.gingko.duominuo.databinding.ActivityRegisterBinding
import com.gingko.duominuo.ui.widget.MCountDownTimer
import com.gingko.duominuo.utils.StatusBarUtil
import com.gingko.duominuo.utils.isMobile
import com.gingko.duominuo.utils.singleClick
import com.gingko.duominuo.utils.toast
import com.gingko.duominuo.viewmodel.BaseViewModel
import com.gingko.duominuo.viewmodel.RegisterViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class RegisterActivity : BaseActivity() {
    private val mViewModel by viewModels<RegisterViewModel>()

    private val mViewBinding by lazy { ActivityRegisterBinding.inflate(layoutInflater) }

    private val mCountDownTimer by lazy {
        MCountDownTimer(mViewBinding.tvSendCode, 60000, 1000)
    }

    override fun getContentView(): View {
        return mViewBinding.root
    }

    override fun getViewModel(): BaseViewModel {
        return mViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setDarkMode(this);
        mViewBinding.etPhoneNumber.doAfterTextChanged {
            validSendCode()
            validLoginCondition()
        }
        mViewBinding.etVerifyCode.doAfterTextChanged {
            validLoginCondition()
        }

        mViewBinding.cbReadAndAgree.setOnCheckedChangeListener { _, _ ->
            validLoginCondition()
        }

        mViewBinding.tvUserAgreement.singleClick {
        }
        mViewBinding.tvPrivacyPolicy.singleClick {
        }
        mViewBinding.tvSendCode.singleClick {
            val phoneText = mViewBinding.etPhoneNumber.text
            if (phoneText.isNullOrEmpty()) {
                getString(R.string.please_input_phone_number).toast()
            } else if (!phoneText.toString().isMobile()) {
                getString(R.string.please_input_correct_phone_number).toast()
            } else {
                mViewModel.sendSmsCode(
                    mViewBinding.etPhoneNumber.text.toString()
                )
            }
        }

        mViewBinding.btnRegister.singleClick {
            val userName = mViewBinding.etUserName.text.toString()
            val phoneNumber = mViewBinding.etPhoneNumber.text.toString()
            val verifyCode = mViewBinding.etVerifyCode.text.toString()
            mViewModel.register(RegisterReqVo(userName, phoneNumber, verifyCode))
        }

        mViewModel.sendCodeLiveData.observe(owner = this) {
            when (it) {
                is Resource.Success -> {
                    mCountDownTimer.start()
                }
                is Resource.Error -> {
                    it.throwable?.message?.toast()
                }
            }
        }
        mViewModel.registerLiveData.observe(owner = this) {
            when (it) {
                is Resource.Success -> {
                    "请等待后台人工审核成功后登录".toast()
                    this@RegisterActivity.finish()
                }
                is Resource.Error -> {
                    it.throwable?.message?.toast()
                }
            }
        }
    }

    private fun validSendCode() {
        val isEmptyPhoneNumber = mViewBinding.etPhoneNumber.text.isNullOrEmpty()
        val isMobile = mViewBinding.etPhoneNumber.text.toString().isMobile()
        if (mViewBinding.tvSendCode.isClickable) {
            if (!isEmptyPhoneNumber && isMobile) {
                mViewBinding.tvSendCode.isEnabled = true
                mViewBinding.tvSendCode.setTextColor(ContextCompat.getColor(App.instance, R.color.textLink))
            } else {
                mViewBinding.tvSendCode.isEnabled = false
                mViewBinding.tvSendCode.setTextColor(ContextCompat.getColor(App.instance, R.color.textHint))
            }
        }
    }

    private fun validLoginCondition() {
        val isEmptyUserName = mViewBinding.etUserName.text.isNullOrEmpty()
        val isEmptyPhoneNumber = mViewBinding.etPhoneNumber.text.isNullOrEmpty()
        val isMobile = mViewBinding.etPhoneNumber.text.toString().isMobile()
        val isEmptyVerifyCode = mViewBinding.etVerifyCode.text.isNullOrEmpty()
        val isCheckProtocol = mViewBinding.cbReadAndAgree.isChecked
        mViewBinding.btnRegister.isEnabled =
            !isEmptyUserName && !isEmptyPhoneNumber && isMobile && !isEmptyVerifyCode && isCheckProtocol
    }

    override fun onDestroy() {
        mCountDownTimer.cancel()
        super.onDestroy()
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, RegisterActivity::class.java)
            context.startActivity(starter)
        }
    }

}